import { moveTo} from '../../../../api/point'
import {findByMobile} from '../../../../api/member'
const util = require('../../../../utils/util.js')
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maxlength: 11,
    mchId:app.globalData.mchId,
    mobile:'',
    amount:'',
    point:'',//红包余额
    member:{
      mobile:'',
      nickname:'',
      avatar:''
    },
    isLoading:false,
    iscurrentMobile:false,
    msg:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.point = options.point
    this.setData({
      point:this.data.point
    })
  },
  getInputPhone(e) {
    this.data.mobile = e.detail.value
  },
  getAmount(e) {
    this.data.amount = e.detail.value
  },
  cashOut () {
    var _this = this;
		if(util.isNull(this.data.mobile)){
			wx.showToast({ title: '请输入手机号', icon: 'none', duration: 2000 })
			return
		}
		if(util.isNull(this.data.amount)){
			wx.showToast({ title: '请输入转赠金额', icon: 'none', duration: 2000 })
			return
    }

    // if(this.data.point >= 0.00){
		// 	wx.showToast({ title: '红包余额需大于0', icon: 'none', duration: 2000 })
		// 	return
    // }    

    if(!this.data.isLoading){
      this.setLoading();
      if(this.data.iscurrentMobile){
        wx.showModal({
          title: '红包转赠提示',
          content: '您将为手机号：' + this.data.member.mobile +'用户转赠'+this.data.amount + '元红包，确定是否继续？',
          success (res) {
            if (res.confirm) {
              moveTo({
                amount:_this.data.amount,
                mchId:_this.data.mchId,
                mobile:_this.data.member.mobile,
              }).then(res=>{
                _this.data.isLoading = false
                _this.setData({
                  mobile:'',
                  amount:'',
                  isLoading:false,
                  iscurrentMobile:false
                })
                if(res.type=='success'){
                  // wx.showToast({ title: '您已经赠送成功', icon: 'success'})
                  _this.toBack();
                } else {
                  wx.showToast({ title: res.content, icon: 'success'})
                }
              }).catch(error=>{
                // var msg = error.content
                // wx.showModal({
                //   title: '提示',
                //   content: error.content,
                //   showCancel:false
                // })
                   _this.data.isLoading = false
                      _this.setData({
                        isLoading:false
                      })
                // wx.showModal({
                //   title: '温馨提示',
                //   content: msg +'，即将退出',
                //   success (res) {
                //     if (res.confirm) {
                //       _this.toBack();
                //     } else if (res.cancel) {
                //       _this.data.isLoading = false
                //       _this.setData({
                //         mobile:'',
                //         amount:'',
                //         isLoading:false
                //       })
                //     }
                //   }
                // })
              })
            } else if (res.cancel) {
                wx.showModal({
                  title: '提示',
                  content: '您已取消转赠',
                  showCancel:false
                })
                _this.toBack()
            }
          }
        })
        
      }else{
        wx.showToast({ title: this.data.msg, icon: 'error'})
        _this.setData({
          isLoading:false
        })
      }
    }
    else{
      wx.showModal({
        title: '提示',
        content: '请勿重复提交',
        showCancel:false
      })
    }
		
  },
  setLoading() {
    this.data.isLoading = true
    this.setData({
      isLoading: true
    })
  },
  toBack(){
      wx.navigateBack({
        delta: 1
      })
  },
  setAllAmount(){
    let _this = this;
    _this.data.amount = _this.data.point
    _this.setData({
      amount:_this.data.amount
    })
  },
  findByMobile(e){
    let mobile = this.data.mobile
    if(mobile){
        findByMobile({
          // mchId:this.data.mchId,
          mobile:mobile
        }).then(res =>{
        if(res.type == 'success'){
          this.data.member.mobile = res.data.member.mobile
          this.data.member.nickname = res.data.nickname
          this.data.member.avatar = res.data.avatar
          this.setData({
            mobile:e.detail.value,
            member:this.data.member,
            iscurrentMobile:true
          })
        }
      })
      .catch(error =>{
        this.clearInput()
        this.setData({
          msg:error.content
        })
      })
    }
    },
    clearInput(){
      this.data.member.mobile = ''
      this.data.member.nickname = ''
      this.data.member.avatar = ''
      this.setData({
        member:this.data.member,
        iscurrentMobile:false
      })
    }
    
})